package cn.pconline.autoclub.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.pconline.autoclub.entity.Activity;
import cn.pconline.autoclub.entity.ActivityPic;

/**
 * 活动图片service
 * @author fgs
 *
 */
public class ActivityPicService {

    @Autowired
    GeliDao geliDao;
    @Autowired
    GeliOrm geliOrm;
    @Autowired
    JdbcTemplate jdbcTemplate;
    
	private static final Logger log = LoggerFactory.getLogger(ActivityPicService.class);
	
	
    /**
     * 批量插入活动图片
     * @param activityId
     * @param pics
     * @return
     */
    public int batchCreateActivityPic(long activityId,List<ActivityPic> pics){
 	   if( pics == null || pics.isEmpty()){
           return 0;
       }
 	  SqlBuilder sqlBuilder = new SqlBuilder();
 	  sqlBuilder.appendSql(" insert into ").appendSql(geliOrm.getTableName(ActivityPic.class));
 	  sqlBuilder.appendSql(" (pic_id,activity_id,url,width,height,seq,description) value(?,?,?,?,?,?,?)");
 	  List<Object[]> batch = new ArrayList<Object[]>();
 	  for(ActivityPic pic : pics){
 		 Object[] values = new Object[]{geliDao.generateId(ActivityPic.class),activityId,pic.getUrl(),pic.getWidth(),pic.getHeight(),pic.getSeq(),pic.getDescription()};
 		 batch.add(values);
 	  }
 	  return jdbcTemplate.batchUpdate(sqlBuilder.getSql(),batch).length;
    }
    
    /**
     * 先删除就图片再插图新图片
     * @param activityId
     * @param pics
     * @return
     */
    public int deleteAndAddActivityPic(long activityId,List<ActivityPic> pics){
    	deleteActivityPicsByActivityId(activityId);
    	return batchCreateActivityPic(activityId, pics);
    }
    
    /**
     * 根据activityId删除所属的活动图片
     * @param activityId
     */
    public int  deleteActivityPicsByActivityId(long activityId){
    	SqlBuilder sqlBuilder = new SqlBuilder();
    	sqlBuilder.appendSql(" delete from ").appendSql(geliOrm.getTableName(ActivityPic.class));
    	sqlBuilder.appendSql(" where activity_id = ").appendValue(activityId);
    	return geliDao.getJdbcTemplate().update(sqlBuilder.getSql(), sqlBuilder.getValues());
    }
    /**
     * 根据活动排序获取活动图片
     * @param activity	活动
     * @param orderType	排序方式（asc : 升序，desc:倒序）
     * @return
     */
    public List<ActivityPic> listPicsByActivity(Activity activity,String orderType){
    	if(activity==null || activity.getActivityId() <=0) return Collections.emptyList();
    	long activityId = activity.getActivityId();
    	SqlBuilder sqlBuilder = new SqlBuilder();
    	sqlBuilder.appendSql(" select pic_id from ").appendSql(geliOrm.getTableName(ActivityPic.class));
    	sqlBuilder.appendSql(" where activity_id = ").appendValue(activityId);
    	sqlBuilder.appendSql(" order by seq ").appendSql(orderType);
    	try {
    		List<ActivityPic> list = geliDao.list(ActivityPic.class,sqlBuilder.getSql(), sqlBuilder.getValues());
    		return list;
        }
        catch(EmptyResultDataAccessException e ) {
        	return Collections.emptyList();
        }
    }
}
